package com.tancw.cloud.product.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.tancw.cloud.common.Result;
import com.tancw.cloud.product.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 商品控制器
 */
@Slf4j
@RestController
@RequestMapping
public class ProductController {

    @GetMapping("/{id}")
    @SentinelResource(value = "getProductById")
    public Result<Product> getProductById(@PathVariable Long id) {
        log.info("查询商品: {}", id);
        
        // TODO: 从数据库查询商品
        Product product = new Product();
        product.setId(id);
        product.setProductName("测试商品");
        product.setProductCode("P" + System.currentTimeMillis());
        product.setPrice(BigDecimal.valueOf(99.99));
        product.setStock(100);
        product.setStatus(1);
        product.setCreateTime(LocalDateTime.now());
        
        return Result.success(product);
    }

    @PostMapping
    public Result<Void> createProduct(@RequestBody Product product) {
        log.info("创建商品: {}", product.getProductName());
        // TODO: 保存商品到数据库
        return Result.success();
    }

    @PutMapping("/stock/deduct")
    @SentinelResource(value = "deductStock")
    public Result<Void> deductStock(@RequestParam Long productId, @RequestParam Integer quantity) {
        log.info("扣减库存: productId={}, quantity={}", productId, quantity);
        // TODO: 扣减商品库存
        return Result.success();
    }
}
